package gamagora.box2d
{
	import Box2D.Dynamics.b2Body;
	
	import org.flixel.FlxGroup;
	import org.flixel.FlxSprite;
	import org.flixel.FlxState;
	
	public class OSPhysicSprite extends FlxSprite
	{
		protected var _startX:Number;
		protected var _startY:Number;
		protected var _physic:OSPhysicObject;
		protected var _data:OSPhysicData;
		protected var _layer:uint;
		protected var _nbSprites:uint;
		protected var _inGame:Boolean;
		protected var _physicWidth:Number;
		protected var _physicHeight:Number;
		
		public function OSPhysicSprite(X:Number=0, Y:Number=0, simpleGraphic:Class=null)
		{
			super(X, Y, simpleGraphic);
			_startX = X;
			_startY = Y;
			_physic = new OSPhysicObject();
			_nbSprites = 0;
		}
		
		public function create(group:FlxGroup):void
		{
			_inGame = true;
		}
			
		public function destroyPhysic():void
		{
			if(_physic.body != null)
			{
				OSPhysicWorld.destroy(_physic.body);
				_physic.body = null;
			}
			_inGame = false;
		}
		
		public function setData(type:uint):OSPhysicData
		{
			_data = new OSPhysicData(type, this);
			return _data;
		}
		
		public function onBeginContact(sprite:OSPhysicSprite):void
		{
			
		}
		
		public function onEndContact(sprite:OSPhysicSprite):void
		{
			
		}
		
		public function resetPosition():void
		{
			x = _startX;
			y = _startY;
		}
		
		//Override from FlxSprite
		
		public override function destroy():void
		{
			super.destroy();
			destroyPhysic();
		}
		
		public override function kill():void
		{
			super.kill();
			if(_physic.body != null)
			{
				_physic.body.SetActive(false);	
			}
		}
		
		public override function reset(x:Number, y:Number):void
		{
			super.reset(x, y);
			if(_physic.body != null)
			{
				_physic.body.SetActive(true);	
			}
		}
		
		//Getters Setters
		
		public function getPhysic():OSPhysicObject
		{
			return _physic;
		}
		
		public function getBody():b2Body
		{
			return _physic.body;
		}

		public function get nbSprites():uint
		{
			return _nbSprites;
		}

		public function set nbSprites(value:uint):void
		{
			_nbSprites = value;
		}

		public function get inGame():Boolean
		{
			return _inGame;
		}

		public function get data():OSPhysicData
		{
			return _data;
		}

		public function get physicWidth():Number
		{
			return _physicWidth;
		}

		public function set physicWidth(value:Number):void
		{
			_physicWidth = value;
		}

		public function get physicHeight():Number
		{
			return _physicHeight;
		}

		public function set physicHeight(value:Number):void
		{
			_physicHeight = value;
		}
	}
}
